package pwdMD5;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class SHA256Example {
    public static void main(String[] args) {
        String data = "123456";
        String hashedData1 = sha256(sha256(data));
        String hashedData2 = sha256(sha256(data));
        String salt1 = salt(hashedData1);
        String salt2 = salt(hashedData2);
        System.out.println(salt1);
       // System.out.println(salt1.equals(salt2));
        // 打印加密后的值
//        System.out.println(hashedData1.equals(hashedData2));
//        System.out.println(hashedData1);
    }
    // 方法用于计算字符串的SHA-256哈希值，并返回十六进制字符串
    public static String sha256(String data) {
        try {
            // 创建SHA-256加密对象
            MessageDigest digest = MessageDigest.getInstance("SHA-256");

            // 计算消息摘要
            byte[] hash = digest.digest(data.getBytes());

            // 将字节数组转换为十六进制字符串
            StringBuilder hexString = new StringBuilder();
            for (byte b : hash) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }

            // 返回加密后的十六进制字符串
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }
    private static String salt(String data){
        char[] arr = data.toCharArray();
        StringBuilder sb=new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            sb.append( (char) (arr[i] ^ 1) );
        }
        return sb.toString();
    }


}
